#ifndef OWN_THUNK_THIS_TO_CDECL_H_080325
#define OWN_THUNK_THIS_TO_CDECL_H_080325

#include <Thunk/ThunkBase.h>

#include <Thunk/MachineCodeMacro.h>

namespace Thunk
{
	
#define __THIS_TO__CDECL_CODES()			\
/*	MOV DWORD PTR [old_esp],ESP	*/			\
CONST	CODE_FIRST(word,MOV_ESP_TO,0x2589)	\
CONST	CODE(dword_ptr,pold_esp,&old_esp)	\
											\
/*	POP	ECX	*/								\
CONST	CODE(byte,POP_ECX,0x59)				\
											\
/*	MOV DWORD PTR [old_return],ECX	*/		\
CONST	CODE(word,MOV_POLD_R,0x0D89)		\
CONST	CODE(dword_ptr,p_old_return,&old_return)	\
											\
/*	MOV ECX,this	*/						\
CONST	CODE(byte,MOV_ECX,0xB9)				\
		CODE(dword_ptr,m_this,0)			\
											\
/*	CALL memFunc	*/						\
CONST	CODE(byte,CALL,0xE8)				\
		CODE(dword,m_memFunc,0)				\
											\
/*	MOV ESP,old_esp	*/						\
CONST	CODE(byte,MOV_ESP,0xBC)				\
CONST	CODE(dword,old_esp,0)				\
/*	MOV DWORD PTR [ESP],old_retrun	*/		\
CONST	CODE(word,MOV_P,0x04C7)				\
CONST	CODE(byte,_ESP,0x24)				\
CONST	CODE(dword,old_return,0)			\
/*	RET	*/									\
CONST	CODE(byte,RET,0xC3)


	class ThisToCdecl
	{
	public:
		~ThisToCdecl();
		explicit ThisToCdecl(const void *Obj=0,int memFunc=0);
	
		const void* Attach(const void *newObj);
		int Attach(int newFunc);

	private:

		void SetOffset(dword dst);
		dword GetDST();

#pragma pack( push , 1)

	__THIS_TO__CDECL_CODES()

#pragma pack( pop )

	};
}

#endif	//#ifndef OWN_THUNK_THIS_TO_CDECL_H_080325
